package com.demo.core.context.event;

import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationListener;

import com.demo.core.context.RefreshModel;
import com.demo.core.context.Refreshable;

/**
 * 重置安全资源元数据监听器
 * @author ryuu.kk
 *
 */
public class RefreshSecurityMetadataSourceListener implements ApplicationListener<RefreshEvent>  {

	/**
	 * 可刷新资源Map
	 * key 为类型type
	 */
	private Map<String, List<Refreshable>> refreshMap;
	
	@Override
	public void onApplicationEvent(RefreshEvent event) {
		RefreshModel model = event.getRefreshModel();
		String type = model.getType();
		if (refreshMap != null && refreshMap.containsKey(type)) {
			List<Refreshable> refreshList = refreshMap.get(type);
			for (Refreshable r : refreshList) {
				if (event.getTimestamp() > r.getLastRefreshTime()) {
					r.refresh();
				}
			}
		}
	}

	public void setRefreshMap(Map<String, List<Refreshable>> refreshMap) {
		this.refreshMap = refreshMap;
	}
}
